<?php
defined('_VISIT_URL_') or exit('No direct script access allowed');

class housecraft {

	//根据条件重置家政服务列表（wap端专用）
	public function resetHousecraftList() {
		$sortation = getPost('sortation');
        $keyword = getPost('keyword');
        $page = getPost('page', '1');

        if (intval($page) > 10) {
            $page = '10';
        }

        if ($sortation != '' && $sortation != '0' && $sortation != '1' && $sortation != '2') {
            return util::getValidError('栏目分类格式错误');
        }

        if (!empty($keyword) && valid::isWicked($keyword)) {
            return util::getValidError('关键词格式错误');
        }

        $queryitem = '';
        if ($sortation != '')
            $queryitem .= ' and sortation = ' . $sortation;
        if ($keyword != '')
            $queryitem .= " and title like '%" . $keyword . "%'";

        $sortOption = ' istop desc, sort desc, id desc';
        $limitOption = ' limit 0, ' . ($page * _LIST_PAGE_ROWS_);

        $rst = new sqlcon();

        //获取家政服务信息总数
        $info = $rst->getInfo('count(*) as num', 'housecraft', 'status = 1' . $queryitem);

        //获取家政服务信息列表
        $list = $rst->getList(
            'housecraftid, title, sortation, identity, feature, area, smallpic, istop, clicktime, dialtime, refreshtime', 
            'housecraft', 
            'status = 1' . $queryitem, 
            $sortOption . $limitOption
        );
        for ($i = 0; $i < count($list); $i++) {
        	//格式化特征列表
            if (!empty($list[$i]['feature'])) {
                $list[$i]['feature'] = explode('，', $list[$i]['feature']);
            }

            //将区域中的逗号替换为空格，并将“区/县”替换为空
            if (!empty($list[$i]['area'])) {
            	$list[$i]['area'] = str_replace('，', ' ', $list[$i]['area']);
	            $list[$i]['area'] = str_replace('区', '', $list[$i]['area']);
	            $list[$i]['area'] = str_replace('县', '', $list[$i]['area']);
	            $list[$i]['area'] .= ' 可服务';
            }

            //格式化列表小图路径
        	if (!empty($list[$i]['smallpic'])) {
                $list[$i]['smallpic'] = _HTTP_PATH_ . $list[$i]['smallpic'];
            }
            else {
                $list[$i]['smallpic'] = 'static/pic/pc/logo_default.jpg';
            }

            //计算浏览 / 电话 / 更新时间与当前时间的差值
            $nowtime = strtotime(date('Y-m-d H:i:s'));
            $seconds = '';
            $moveAbout = '';
            if (!empty($list[$i]['clicktime']) || !empty($list[$i]['dialtime'])) {
            	$clicktime = strtotime($list[$i]['clicktime']);
            	$dialtime = strtotime($list[$i]['dialtime']);
            	if ($clicktime > $dialtime) {
            		//显示浏览时间
            		$seconds = abs($nowtime - $clicktime);
            		$moveAbout = '有人浏览';
            	}
            	else {
            		//显示电话时间
            		$seconds = abs($nowtime - $dialtime);
            		$moveAbout = '有人联系';
            	}
            }
            else {
            	//浏览与电话时间都为空，则显示更新时间
            	$refreshtime = strtotime($list[$i]['refreshtime']);
            	$seconds = abs($nowtime - $refreshtime);
            	$moveAbout = '更新';
            }

            //格式化浏览 / 电话 / 更新时间
            $minutes = $seconds / 60;
        	if ($minutes > 59) {
    			$hour = $minutes / 60;
    			if ($hour > 23) {
    				$day = $hour / 24;
    				$list[$i]['difftime'] = ceil($day) . '天前';
    			}
    			else {
    				$list[$i]['difftime'] = ceil($hour) . '小时前';
    			}
    		}
    		else {
    			$list[$i]['difftime'] = ceil($minutes) . '分钟前';
    		}
    		$list[$i]['difftime'] .= $moveAbout;
        }

        $result = util::getSuccess('success');
        $result['listCount'] = $info['num'];
        $result['allPages'] = util::getListPage($info['num'], _LIST_PAGE_ROWS_);
        $result['housecraftList'] = $list;
        $result['rowCount'] = _LIST_PAGE_ROWS_;
        return $result;
	}

	//获取家政服务列表
	public function getHousecraftList() {
		$sortation = getPost('sortation');
        $keyword = getPost('keyword');
        $page = getPost('page', '1');

        if ($sortation != '' && $sortation != '0' && $sortation != '1' && $sortation != '2') {
            return util::getValidError('栏目分类格式错误');
        }

        if (!empty($keyword) && valid::isWicked($keyword)) {
            return util::getValidError('关键词格式错误');
        }

        $queryitem = '';
        if ($sortation != '')
            $queryitem .= ' and sortation = ' . $sortation;
        if ($keyword != '')
            $queryitem .= " and title like '%" . $keyword . "%'";

        $sortOption = ' istop desc, sort desc, id desc';
        $limitOption = ' limit ' . ($page - 1) * _LIST_PAGE_ROWS_ . ', ' . _LIST_PAGE_ROWS_;

        $rst = new sqlcon();

        //获取家政服务信息总数
        $info = $rst->getInfo('count(*) as num', 'housecraft', 'status = 1' . $queryitem);

        //获取家政服务信息列表
        $list = $rst->getList(
            'housecraftid, title, sortation, identity, feature, area, smallpic, istop, clicktime, dialtime, refreshtime', 
            'housecraft', 
            'status = 1' . $queryitem, 
            $sortOption . $limitOption
        );
        for ($i = 0; $i < count($list); $i++) {
        	//格式化特征列表
            if (!empty($list[$i]['feature'])) {
                $list[$i]['feature'] = explode('，', $list[$i]['feature']);
            }

            //将区域中的逗号替换为空格，并将“区/县”替换为空
            if (!empty($list[$i]['area'])) {
            	$list[$i]['area'] = str_replace('，', ' ', $list[$i]['area']);
	            $list[$i]['area'] = str_replace('区', '', $list[$i]['area']);
	            $list[$i]['area'] = str_replace('县', '', $list[$i]['area']);
	            $list[$i]['area'] .= ' 可服务';
            }

            //格式化列表小图路径
        	if (!empty($list[$i]['smallpic'])) {
                $list[$i]['smallpic'] = _HTTP_PATH_ . $list[$i]['smallpic'];
            }
            else {
                $list[$i]['smallpic'] = 'static/pic/pc/logo_default.jpg';
            }

            //计算浏览 / 电话 / 更新时间与当前时间的差值
            $nowtime = strtotime(date('Y-m-d H:i:s'));
            $seconds = '';
            $moveAbout = '';
            if (!empty($list[$i]['clicktime']) || !empty($list[$i]['dialtime'])) {
            	$clicktime = strtotime($list[$i]['clicktime']);
            	$dialtime = strtotime($list[$i]['dialtime']);
            	if ($clicktime > $dialtime) {
            		//显示浏览时间
            		$seconds = abs($nowtime - $clicktime);
            		$moveAbout = '有人浏览';
            	}
            	else {
            		//显示电话时间
            		$seconds = abs($nowtime - $dialtime);
            		$moveAbout = '有人联系';
            	}
            }
            else {
            	//浏览与电话时间都为空，则显示更新时间
            	$refreshtime = strtotime($list[$i]['refreshtime']);
            	$seconds = abs($nowtime - $refreshtime);
            	$moveAbout = '更新';
            }

            //格式化浏览 / 电话 / 更新时间
            $minutes = $seconds / 60;
        	if ($minutes > 59) {
    			$hour = $minutes / 60;
    			if ($hour > 23) {
    				$day = $hour / 24;
    				$list[$i]['difftime'] = ceil($day) . '天前';
    			}
    			else {
    				$list[$i]['difftime'] = ceil($hour) . '小时前';
    			}
    		}
    		else {
    			$list[$i]['difftime'] = ceil($minutes) . '分钟前';
    		}
    		$list[$i]['difftime'] .= $moveAbout;
        }

        $result = util::getSuccess('success');
        $result['listCount'] = $info['num'];
        $result['allPages'] = util::getListPage($info['num'], _LIST_PAGE_ROWS_);
        $result['housecraftList'] = $list;
        $result['rowCount'] = _LIST_PAGE_ROWS_;
        return $result;
	}

	//获取家政服务信息
    public function getHousecraftInfo() {
    	if (empty(getPost('housecraftid')) || valid::isWicked(getPost('housecraftid')) || strlen(getPost('housecraftid')) != 32) {
            return util::getValidError('ID格式错误');
        }

        $rst = new sqlcon();

        $info = $rst->getInfo(
            'housecraftid, title, company_verifyid, sortation, address, position, person, phone, identity, feature, area, introduce, clicktime, dialtime, refreshtime', 
            'housecraft', 
            "status = 1 and housecraftid = '" . getPost('housecraftid') . "'"
        );
        if (empty($info)) {
            return util::getDataError('信息不存在');
        }

        //格式化特征列表
        if (!empty($info['feature'])) {
            $info['feature'] = explode('，', $info['feature']);
        }

        //将区域中的逗号替换为空格，并将“区/县”替换为空
        if (!empty($info['area'])) {
        	$info['area'] = str_replace('，', ' ', $info['area']);
	        $info['area'] = str_replace('区', '', $info['area']);
	        $info['area'] = str_replace('县', '', $info['area']);
	        $info['area'] .= ' 可服务';
        }

        //获取并格式化服务详情中的换行
        if (!empty($info['introduce'])) {
            $file_path = _FILE_PATH_ . $info['introduce'];
            $info['introduce'] = util::readFile($file_path);
            if (!empty($info['introduce'])) {
                $info['introduce'] = nl2br($info['introduce']);
            }
        }

        //获取商家图片
        $picList = $rst->getList('path', 'housecraftpic', "housecraftid = '" . getPost('housecraftid') . "'", 'sx asc');
        for ($i = 0; $i < count($picList); $i++) {
            $picList[$i]['name'] = time() . $i;
            $picList[$i]['path'] = _HTTP_PATH_ . $picList[$i]['path'];
        }
        $info['picList'] = $picList;

        //计算浏览 / 电话 / 更新时间与当前时间的差值
        $nowtime = strtotime(date('Y-m-d H:i:s'));
        $seconds = '';
        $moveAbout = '';
        if (!empty($info['clicktime']) || !empty($info['dialtime'])) {
            $clicktime = strtotime($info['clicktime']);
            $dialtime = strtotime($info['dialtime']);
            if ($clicktime > $dialtime) {
                //显示浏览时间
                $seconds = abs($nowtime - $clicktime);
                $moveAbout = '有人浏览';
            }
            else {
                //显示电话时间
                $seconds = abs($nowtime - $dialtime);
                $moveAbout = '有人联系';
            }
        }
        else {
            //浏览与电话时间都为空，则显示更新时间
            $refreshtime = strtotime($info['refreshtime']);
            $seconds = abs($nowtime - $refreshtime);
            $moveAbout = '更新';
        }

        //格式化浏览 / 电话 / 更新时间
        $minutes = $seconds / 60;
        if ($minutes > 59) {
            $hour = $minutes / 60;
            if ($hour > 23) {
                $day = $hour / 24;
                $info['difftime'] = ceil($day) . '天前';
            }
            else {
                $info['difftime'] = ceil($hour) . '小时前';
            }
        }
        else {
            $info['difftime'] = ceil($minutes) . '分钟前';
        }
        $info['difftime'] .= $moveAbout;

        //被点击的家政服务信息点击量+1，且更新最近点击时间
        $rst->setSql("update housecraft set click = click + 1, clicktime = '" . date('Y-m-d H:i:s') . "' where housecraftid = '" . $info['housecraftid'] . "'");
        $rst->execute();

        $result = util::getSuccess('success');
        $result['housecraftInfo'] = $info;
        return $result;
    }

    //设置家政服务信息的电话量与最新电话时间
    public function setHousecraftDial() {
        if (empty(getPost('housecraftid')) || valid::isWicked(getPost('housecraftid')) || strlen(getPost('housecraftid')) != 32) {
            return util::getValidError('ID格式错误');
        }

        $rst = new sqlcon();

        $rst->setSql("update housecraft set dial = dial + 1, dialtime = '" . date('Y-m-d H:i:s') . "' where housecraftid = '" . getPost('housecraftid') . "'");
        $rst->execute();

        return util::getSuccess('success');
    }
	
}